package com.ruike.alisurface.ui.payPage;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.ruike.alisurface.R;
import com.ruike.alisurface.Serials.BackSweepUtils;
import com.ruike.alisurface.Serials.MsgEventBus;
import com.ruike.alisurface.base.BaseAty;
import com.ruike.alisurface.bean.ShopDetailBean;
import com.ruike.alisurface.http.OtherApiHttps;
import com.ruike.alisurface.utils.ShareKey;
import com.voodoo.lib_utils.L;
import com.voodoo.lib_utils.ShareUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author：voodoo
 * CreateTime：2021/10/21 021 上午 10:37:34
 * Description：反扫设备
 */
public class PayForSweepActivity extends BaseAty implements BackSweepUtils.OnScanningResultListener {

    @BindView(R.id.payForSweep_countDownTime_tv)
    TextView countDownTimeTv;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_pay_for_sweep;
    }

    @Override
    protected void initViews() {
        initTitleBar(false);
    }

    @Override
    protected void onStart() {
        super.onStart();
        startCountdownTimer(120, countDownTimeTv, null);

        BackSweepUtils.initListener();
        BackSweepUtils.setOnScanningResultListener(this);
        BackSweepUtils.startScanning();
    }

    @Override
    protected void initData() {
    }

    @Override
    public void doEventBusResult(MsgEventBus event) {
    }

    @OnClick(R.id.payForSweep_goback_imgv)
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.payForSweep_goback_imgv:
                startCountdownTimer(0, null, null);
                break;
        }
    }

    /**
     * 反扫回传的数据
     *
     * @param resultPrintStr 扫码转码之后的明文
     */
    @Override
    public void onResultListener(String resultPrintStr) {
        showProgressDialog("正在核验券码...");
        ShareUtils.getInstance().putString(ShareKey.COUPON_CODE, resultPrintStr);
        OtherApiHttps.startOut(resultPrintStr, this);
    }

    @Override
    public void onSuccess(String requestUrl, String requestJsonStr) {
        super.onSuccess(requestUrl, requestJsonStr);
        if (OtherApiHttps.START_OUT_URL.equals(requestUrl)) {
            try {
                JSONObject jsonResultObject = new JSONObject(requestJsonStr);
                JSONObject dataJson = jsonResultObject.optJSONObject("data");
                if (dataJson == null) {
                    L.e("回传的数据中不存在data");
                    showErrorTip("回传的数据中不存在data");
                    return;
                }
                String orderIdStr = dataJson.optString("id");
                JSONArray jsonDetails = dataJson.optJSONArray("details");
                if (jsonDetails == null) {
                    L.e("回传的数据中不存在货道信息");
                    showErrorTip("回传的数据中不存在货道信息");
                    return;
                }

                ShopDetailBean shopDetailBean = new ShopDetailBean(orderIdStr);
                List<ShopDetailBean.OrderDetailsBean> detailOrderList = new ArrayList<>();
                ShopDetailBean.OrderDetailsBean detailsBean;
                for (int i = 0; i < jsonDetails.length(); i++) {
                    JSONObject detailsJson = (JSONObject) jsonDetails.get(i);
                    detailsBean = new ShopDetailBean.OrderDetailsBean();
                    detailsBean.setQuantity(detailsJson.optInt("quantity"));
                    detailsBean.setSlotIndex(detailsJson.optInt("slotIndex"));
                    detailsBean.setSlotTypeId(detailsJson.optInt("slotTypeId"));
                    detailOrderList.add(detailsBean);
                }
                shopDetailBean.setDetailOrderList(detailOrderList);

                Bundle bundle = new Bundle();
                bundle.putString("pay_type", "sweep");
                bundle.putSerializable("orderBeanData", shopDetailBean);
                startActivity(PayShowShopActivity.class, bundle);
                finish();

            } catch (JSONException e) {
                L.e("回传的JSON数据格式不正确");
                showErrorTip("回传的JSON数据格式不正确");
                BackSweepUtils.startScanning();
            }
        }

    }

    @Override
    public void onError(String requestUrl, String errorMsg) {
        super.onError(requestUrl, errorMsg);
        if (OtherApiHttps.START_OUT_URL.equals(requestUrl)) {
            showErrorTip(errorMsg);
            BackSweepUtils.startScanning();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        // 取消扫描回调
        BackSweepUtils.cancelScanning();
    }

}
